﻿using System;
using System.Reflection;
using System.Windows;
using System.Windows.Input;
using System.Windows.Markup;

// BindingExtensions.cs 
// Contains classes which override ObjectBindingExtension to allow
// for a typed DummyValue - a do-nothing and temporary value which can be 
// legally bound to the typed target property.
namespace PasswordProvider.View.Behaviors
{
    /// <summary>
    /// Bind to a property expecting an ICommand.
    /// </summary>
    [MarkupExtensionReturnType(typeof(ICommand))]
    class CommandBindingExtension : ObjectBindingExtension
    {
        public CommandBindingExtension() { }
        public CommandBindingExtension(string bindingPath)
            : base(bindingPath)
        { }

        /// <summary>
        /// Return an ICommand that does nothing.
        /// </summary>
        public override object DummyValue
        {
            get
            {
                return DummyCommand.Instance;
            }
        }
    
        // A dummy command that does nothing...
        private class DummyCommand : ICommand
        {
            #region Singleton pattern
            private DummyCommand()
            {
            }
            private static DummyCommand _instance = null;
            public static DummyCommand Instance
            {
                get
                {
                    if (_instance == null)
                    {
                        _instance = new DummyCommand();
                    }
                    return _instance;
                }
            }
            #endregion

            #region ICommand Members
            public bool CanExecute(object parameter)
            {
                return false;
            }
            #pragma warning disable 0067
            public event EventHandler CanExecuteChanged;
            #pragma warning restore 0067

            public void Execute(object parameter)
            {
            }
            #endregion
        }
    }

    /// <summary>
    /// Bind to a property expecting an Enum
    /// </summary>
    [MarkupExtensionReturnType(typeof(Enum))]
    class EnumBindingExtension : ObjectBindingExtension
    {
        public EnumBindingExtension() { }
        public EnumBindingExtension(string bindingPath)
            : base(bindingPath)
        { }

        public override object DummyValue
        {
            get
            {
                return null;
            }
        }
    }

    /// <summary>
    /// Bind to a property expecting a string.
    /// </summary>
    [MarkupExtensionReturnType(typeof(string))]
    class StringBindingExtension : ObjectBindingExtension
    {
        public StringBindingExtension() { }
        public StringBindingExtension(string bindingPath)
            : base(bindingPath)
        { }

        public override object DummyValue
        {
            get
            {
                return String.Empty;
            }
        }
    }

}
